package com.zycfc.zsf.boot.job;

import com.dangdang.ddframe.job.reg.zookeeper.*;
import org.apache.curator.framework.recipes.cache.*;
import com.zycfc.zsf.boot.job.common.*;
import com.zycfc.zsf.boot.core.common.*;
import com.dangdang.ddframe.job.executor.handler.*;
import java.util.concurrent.*;
import com.zycfc.zsf.boot.util.time.*;
import com.zycfc.zsf.boot.logback.util.*;
import org.springframework.util.*;
import org.slf4j.*;

public class ZsfJobRestart
{
    private static final Logger log;
    private ZookeeperRegistryCenter zookeeperRegistryCenter;
    private String applicationName;
    private final String PATH;
    private boolean isStart;
    
    public ZsfJobRestart(final ZookeeperRegistryCenter zookeeperRegistryCenter, final String applicationName) {
        this.isStart = false;
        this.zookeeperRegistryCenter = zookeeperRegistryCenter;
        this.applicationName = applicationName;
        this.PATH = String.format("/restart/%s", applicationName);
    }
    
    public void init() {
        NodeCache cache = null;
        try {
            cache = new NodeCache(this.zookeeperRegistryCenter.getClient(), this.PATH);
            cache.start();
            this.addListener(cache);
        }
        catch (Exception e) {
            ZsfJobRestart.log.error(e.getMessage(), (Throwable)e);
        }
    }
    
    private void addListener(final NodeCache cache) {
        final NodeCacheListener listener = () -> {
            TraceUtil.beginLocalTrace("ZsfJobRestart" + DateUtils.getCurDT() + DateUtils.getCurTM());
            try {
                if (!this.isStart) {
                    ZsfJobRestart.log.warn("ZsfJob: ZsfJobRestart isStart is false return!");
                    this.isStart = true;
                    return;
                }
                if (cache.getCurrentData() != null) {
                    final String nodePath = cache.getCurrentData().getPath();
                    final String jobName = new String(cache.getCurrentData().getData());
                    ZsfJobRestart.log.info("ZsfJob: ZsfJobRestart Node changed: name path is [{}], value is [{}]", (Object)nodePath, (Object)jobName);
                    if (StringUtils.isEmpty((Object)jobName)) {
                        ZsfJobRestart.log.warn("ZsfJob: ZsfJobRestart jobName is null return!");
                        return;
                    }
                    this.restartJob(jobName);
                    ZsfJobRestart.log.info("ZsfJob: ZsfJobRestart restart success!!!, jobName [{}]", (Object)jobName);
                }
            }
            catch (Exception e) {
                ZsfJobRestart.log.error("ZsfJob: ZsfJobRestart failure, {}", (Object)e.getMessage(), (Object)e);
            }
            TraceUtil.endTrace();
        };
        cache.getListenable().addListener((Object)listener);
    }
    
    private void restartJob(final String jobName) {
        this.clearExecutePoolIfNecessary(jobName);
        ZsfJobRestart.log.info("ZsfJob: ZsfJobRestart clearExecutePoolIfNecessary success!, jobName [{}]", (Object)jobName);
        final String beanName = JobInfPool.getBeans(jobName);
        final Object bean = SpringContextHelper.getBean(beanName);
        ((ZsfJobBeanProcessor)SpringContextHelper.getBean("zsfJobBeanProcessor", (Class)ZsfJobBeanProcessor.class)).initializationBean(bean, beanName);
    }
    
    private void clearExecutePoolIfNecessary(final String jobName) {
        ExecutorService pool = null;
        try {
            pool = ExecutorServiceHandlerRegistry.getExecutorServiceHandler(jobName, (ExecutorServiceHandler)null);
            if (pool != null && !pool.isShutdown()) {
                try {
                    pool.shutdown();
                    if (!pool.awaitTermination(500L, TimeUnit.MILLISECONDS)) {
                        pool.shutdownNow();
                    }
                }
                catch (InterruptedException e) {
                    ZsfJobRestart.log.error("ZsfJob: ZsfJobRestart clearExecutePoolIfNecessary awaitTermination interrupted: ", (Throwable)e);
                    pool.shutdownNow();
                }
            }
            ExecutorServiceHandlerRegistry.remove(jobName);
        }
        catch (Exception e2) {
            ZsfJobRestart.log.error(e2.getMessage(), (Throwable)e2);
        }
    }
    
    static {
        log = LoggerFactory.getLogger((Class)ZsfJobRestart.class);
    }
}
